
define([
	'vue',
	'config',
	'common',
	'../model/upLoadImg'
	],function(Vue,config,utils,upLoadImg){
	'use strict';

	var consumptionCertificateView=new Vue({
		el:'#app',
		data:{
			userCode:'',				//用户编码
			buyerPhone:'',				//买家手机
			isEixt:'',					//用户是否存在
			commodityName:'',			//商品名称
			consumptionMoney:'',		//消费金额
			id:'',						//消费登记id（重新提交时，此项为必填项)
			userId:'',					//消费用户编号
			consumptionCertificate:'',	//消费凭证（当消费金额大于3000时，此项为必填项）
			physicalPic:'',				//实物照片（当消费金额大于3000时，此项为必填项

			backType:'',

			onprogress:'',
			onprogressAgain:'',

			fileList: {
				consumptionCertificate: '',
				physicalPic: '',
			},

			submitBtn:false
		},
		created:function(){
			utils.userToken();
			utils.pushHistory();

			//判断是否重新提交
			var _this = this;
			_this.id = utils.getQueryVariable('id');
			if(!this.id){
				history.back();
			}else{
				utils.ajax(utils.getWebUrl('declaration/get'),{
					id: _this.id
				},'POST',function(res){
					var status = res.data.status;
					if(status === '1'){
						history.back();
					}
					switch (status) {
						case '0':  //首次提交
							_this.type = 1;
							break;
						case '3':   //重新提交
							_this.type = 2;
							break;
						default:
							_this.type = 1;	
							break;
					}
				})
			}

			var backType = utils.getQueryVariable('backType');
			if(backType.indexOf('#')){
				_this.backType = backType.split('#')[0];
			}else{
				_this.backType = backType;
			}
			// window.addEventListener('popstate',function() {
	  //      		if(_this.backType === '1'){
			// 		//从消费登记第一步进来返回
			// 		utils.appAlert('是否放弃本次操作',function(){
			// 			utils.pushHistory();
			// 		},'取消',function(){
			// 			window.location.href = "shopInfo.html";
			// 		},'确定')
			// 	}else{
			// 		utils.appAlert('是否放弃本次操作',function(){
			// 			utils.pushHistory();
			// 		},'取消',function(){
			// 			window.location.href = "expensesRecord_shop.html";
			// 		},'确定')
			// 	}
		 //    })
		},
		methods:{
			//判断返回
			back:function(){
				
				if(this.backType === '1'){
					//从消费登记第一步进来返回
					utils.appAlert('是否放弃本次操作',null,'取消',function(){
						window.location.href = "shopInfo.html";
					},'确定')
				}else{
					utils.appAlert('是否放弃本次操作',null,'取消',function(){
						window.location.href = "expensesRecord_shop.html";
					},'确定')
					
				}
			},
			//第一次上传
			selectPhoto:function(event){
				var id = event.currentTarget.id;
				var $target = $(event.currentTarget);
				var $targetPic = $(event.currentTarget).parents('.Again').find('.imgAgain');

				$targetPic.show();
				$target.parents('.report-file').hide();
				//console.log(id)
				var pic = upLoadImg.showPic($targetPic,$target);
				// 选择完立即压缩
				this.getFiles(id);
			},
			//再次上传
			selectAgain:function(event){
				var $target = $(event.currentTarget);
				var $targetPic = $target.parent('.imgAgain');
				var pic = upLoadImg.showPic($targetPic,$target);
				$target.parents('.Again').find('.file-prew').addClass('remove');
				var id = $target.parents('.Again').find('.file-prew').attr('id');
				// 选择完立即压缩
				this.getFiles(id);			
			},
			getFiles:function(ele){
				var files;
				var _this = this;
				var element = $('#'+ele);
				if(element.hasClass('remove')){
					files = element.parents('.Again').find('.inputAgain').get(0).files[0];
				}else{
					files = element.get(0).files[0];
				}
				
				if(!files){
					_this.fileList[ele] = '';
				}else{
					lrz(files,{width:640})
					.then(function (rst) {
			        _this.fileList[ele] = rst.base64;
			        })
			        .catch(function (err) {
			        _this.fileList[ele] = '';
			        })
			        .always(function () {
			        });
				}

			},
			toConsumerAudit:function(){
				var _this = this;

				var getPic = function(element,isLoading){
					var pic;
					if(isLoading === "t"){
						pic = $(element).find('.pic').attr('src');
					}else{
						pic = $(element).find('.pic');
					}
					return pic;
				}

				// var getFiles = function(element){
				// 	var files;
				// 	var element = $(element);
				// 	if(element.hasClass('remove')){
				// 		return files = element.parents('.Again').find('.inputAgain').get(0).files[0];
				// 	}else{
				// 		return files = element.get(0).files[0];
				// 	}
				// }

				if($.trim(getPic('#consumptionCertificatePic','t')) === ""){
					utils.appAlert('请上传消费凭证');
                    return;
				}

				if($.trim(getPic('#physicalPicPic','t')) === ""){
					utils.appAlert('请上传实物照片');
                    return;
				}

				if(_this.fileList.consumptionCertificate =='' || _this.fileList.physicalPic ==''){
					utils.appAlert('图片处理中，请稍后...');
					return;
				}

				if (_this.submitBtn) {
					return;
				}

				_this.submitBtn = true;

				// var filesList = {
				// 	consumptionCertificatePic: getFiles('#consumptionCertificate'),
				// 	physicalPic: getFiles('#physicalPic')
				// }

				var onprogress = function(evt){
					var loaded = evt.loaded;     //已经上传大小情况 
					var tot = evt.total;      	 //附件总大小 
					var per = Math.floor(100*loaded/tot);  //已经上传的百分比
				    $('.upLoadimg').css({'height':(100-per)+'%'});
				}

				
				var formData = new FormData();
				$.each(_this.fileList,function(k,p){
					if(p){
						formData.append("imgStr",p);
					}
				})
				upLoadImg.ajax(formData,onprogress,function(res){

					var urls = res.urls;
					getPic('#consumptionCertificatePic').attr('src',urls[0]);
					getPic('#physicalPicPic').attr('src',urls[1]);

					utils.ajax(utils.getWebUrl('declaration/supplement'),{
						consumptionCertificate:urls[0],
						physicalPic:urls[1],
						type:_this.type,
						id:_this.id
					},'POST',function(res){
						window.location.href="consumerAudit.html?id="+res.data.id+'&backType=1';
					},null,null,null,function(res){
						_this.submitBtn = false;
						utils.appAlert(res.msg);
					})
				},null,null,null,function(res){
					_this.submitBtn = false;
					utils.appAlert(res.msg);
				})

					

				// var format = upLoadImg.uploadFile(filesList);
				// upLoadImg.ajax(format,onprogress,function(res){

				// 	var urls = res.urls;

				// 	getPic('#consumptionCertificatePic').attr('src',urls[0]);
				// 	getPic('#physicalPicPic').attr('src',urls[1]);

				// 	utils.ajax(utils.getWebUrl('declaration/supplement'),{
				// 		consumptionCertificate:urls[0],
				// 		physicalPic:urls[1],
				// 		type:_this.type,
				// 		id:_this.id
				// 	},'POST',function(res){
				// 		window.location.href="consumerAudit.html?id="+res.data.id+'&backType=1';
				// 	})
				// })
			}
		}
	})

	return consumptionCertificateView;
});